package com.huatai.saas.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.PaginationUtils;
import com.huatai.saas.dto.AnnouncementInfoDto;
import com.huatai.saas.dto.AnnouncementUpdateDto;
import com.huatai.saas.dto.BaseDto;
import com.huatai.saas.entity.AnnouncementEntity;
import com.huatai.saas.mapper.AnnouncementMapper;
import com.huatai.saas.service.AnnouncementService;
import com.huatai.saas.vo.AnnouncementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @program: nanming-saas
 * @author: zcw
 * @description: 公告类的业务逻辑实现
 * @create: 2024-04-24 14:15
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, AnnouncementEntity> implements AnnouncementService {

	/**
	 * 添加公告内容
	 * @param announcementInfo 公告内容
	 * @return
	 */
	@Override
	@Transactional
	public boolean saveAnnouncement(AnnouncementInfoDto announcementInfo) {
		// 创建公告实体
		AnnouncementEntity announcement = new AnnouncementEntity();
		// 将announcementInfo转为announcement
		BeanUtil.copyProperties(announcementInfo, announcement);
		//执行插入操作，返回受影响的行数
		int row = baseMapper.insert(announcement);
		//如果受影响的行数不等于1，则添加失败
		if(row != 1){
			throw new ServiceException("新增公告失败！");
		}
		return true;
	}

	/**
	 * 查看所有的公告列表信息
	 * @param baseDto 分页信息
	 * @return
	 */
	@Override
	public IPage<AnnouncementVO> getAnnouncementList(BaseDto baseDto) {
		// 设置分页信息
		Page<AnnouncementVO> page = PaginationUtils.getPage(
			// 获取页码
			baseDto.getPage(),
			// 每页显示条数
			baseDto.getSize()
		);
		//查询公告列表信息
		IPage<AnnouncementVO> announcementList = baseMapper.getAnnouncementList(page);
		return announcementList;
	}

	@Override
	public AnnouncementVO getAnnouncementInfo(Long id) {
		//查询公告信息是否存在
		AnnouncementEntity announcement = baseMapper.selectById(id);
		//如果结果为null，说明不存在
		if(announcement == null){
			throw new ServiceException("公告信息不存在！");
		}
		//创建AnnouncementVO对象
		AnnouncementVO announcementVO = new AnnouncementVO();
		//将announcement转为announcementVO最为返回结果
		BeanUtil.copyProperties(announcement, announcementVO);
		return announcementVO;
	}

	@Override
	@Transactional
	public boolean delAnnouncement(Long id) {
		//查询公告信息是否存在
		AnnouncementEntity announcement = baseMapper.selectById(id);
		//如果结果为null，说明不存在
		if(announcement == null){
			throw new ServiceException("公告信息不存在！");
		}
		//如果存在，则执行删除操作，返回受影响的行数
		int row = baseMapper.deleteById(id);
		//如果受影响的行数不等于1，删除失败
		if(row != 1){
			throw new ServiceException("删除公告失败！");
		}
		return true;
	}

	/**
	 * 修改公告信息
	 * @param announcementUpdateDto 修改公告信息的参数
	 * @return
	 */
	@Override
	@Transactional
	public boolean updateAnnouncement(AnnouncementUpdateDto announcementUpdateDto) {
		//根据id查询对应的公告是否还存在
		AnnouncementEntity announcement = baseMapper.selectById(announcementUpdateDto.getId());
		//如果结果为null，则说明该公告不存在
		if(announcement == null){
			throw new ServiceException("该公告信息已不存在！");
		}
		//如果存在，则执行更新操作，返回受影响的行数
		BeanUtil.copyProperties(announcementUpdateDto, announcement);
		int row = baseMapper.updateById(announcement);
		//如果受影响的行数不等于1，说明修改不成功
		if(row != 1){
			throw new ServiceException("修改公告信息失败");
		}
		return true;
	}
}
